package com.gxuwz.eschool;

import androidx.appcompat.app.AppCompatActivity;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.content.res.AppCompatResources;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.gxuwz.eschool.fragment.CardFragment;
import com.gxuwz.eschool.fragment.HomeFragment;
import com.gxuwz.eschool.fragment.MessageFragment;
import com.gxuwz.eschool.fragment.PersonFragment;

import java.util.Arrays;
import java.util.List;
//主页面底部导航栏相关
public class MainActivity extends AppCompatActivity {

    private TextView tv_home;
    private TextView tv_card;
    private TextView tv_message;
    private TextView tv_person;
    private HomeFragment homeFragment;
    private CardFragment cardFragment;
    private MessageFragment messageFragment;
    private PersonFragment personFragment;
    private FragmentTransaction fragmentTransaction;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始话控件
        tv_home = findViewById(R.id.tv_home);
        tv_card = findViewById(R.id.tv_card);
        tv_message = findViewById(R.id.tv_message);
        tv_person = findViewById(R.id.tv_person);

        //创建Fragment
        homeFragment = new HomeFragment();
        cardFragment = new CardFragment();
        messageFragment = new MessageFragment();
        personFragment = new PersonFragment();
        setFragment(homeFragment,tv_home,R.drawable.home);
//        setFragment(homeFragment, tv_home,R.drawable.home);



        // 为每个TextView设置点击监听器
        setupClickListener(tv_home,R.drawable.home_no,R.drawable.home);
        setupClickListener(tv_card,R.drawable.schoolcard_no,R.drawable.schoolcard_yes);
        setupClickListener(tv_message,R.drawable.information_no,R.drawable.information_yes);
        setupClickListener(tv_person,R.drawable.mine_no,R.drawable.mine_yes);
    }

    private void setupClickListener(final TextView textView,int defaultIconResId,int selectedIconResId) {
        textView.setCompoundDrawablesWithIntrinsicBounds(0, defaultIconResId, 0, 0); // 设置默认图标
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 改变颜色并设置Fragment
                setFragmentBasedOnTextView(textView,selectedIconResId);
            }
        });
    }

    private void setFragmentBasedOnTextView(TextView textView,int selectedIconResId) {
        switch (textView.getId()) {
            case R.id.tv_home:
                setFragment(homeFragment, textView,selectedIconResId);
                break;
            case R.id.tv_card:
                setFragment(cardFragment, textView,selectedIconResId);
                break;
            case R.id.tv_message:
                setFragment(messageFragment, textView,selectedIconResId);
                break;
            case R.id.tv_person:
                setFragment(personFragment, textView,selectedIconResId);
                break;
        }
    }

    private void setFragment(Fragment fragment, TextView selectedTextView, int selectedIconResId) {

        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.layout_container, fragment);
        fragmentTransaction.commit();

        // 更新图标
        updateTextViewIcons(selectedTextView, selectedIconResId);

    }


    private void updateTextViewIcons(TextView selectedTextView, int selectedIconResId) {
        int defaultColor = Color.GRAY; // 默认颜色
        int selectedTextColor = Color.RED; // 选中文本颜色（示例）

        // 更换选中TextView的图标和文本颜色
        Drawable icon = AppCompatResources.getDrawable(this, selectedIconResId);
        selectedTextView.setCompoundDrawablesWithIntrinsicBounds(null, icon, null, null);
        selectedTextView.setTextColor(selectedTextColor);

        // 重置其他TextView的图标和文本颜色为默认状态
        List<TextView> textViewList = Arrays.asList(tv_home, tv_card, tv_message, tv_person);
        for (TextView tv : textViewList) {
            if (tv != selectedTextView) {
                tv.setTextColor(defaultColor); // 文本颜色重置为默认
                Drawable defaultIcon = AppCompatResources.getDrawable(this, getDefaultIconResIdForTextView(tv));
                tv.setCompoundDrawablesWithIntrinsicBounds(null, defaultIcon, null, null);
            }
        }
    }

    private int getDefaultIconResIdForTextView(TextView tv) {
        if (tv == tv_home) return R.drawable.home_no;
        if (tv == tv_card) return R.drawable.schoolcard_no;
        if (tv == tv_message) return R.drawable.information_no;
        if (tv == tv_person) return R.drawable.mine_no;
        return 0; // 默认返回0，
    }

}